﻿namespace TianYu.Core.Common
{
    public class LogContentBuilder
    {
        private List<string> _logs = new List<string>();

        public LogContentBuilder Append(string fieldName, string oldValue, string newValue)
        {
            var log = fieldName.ToLog(oldValue, newValue);
            if (log.IsNotNullAndWhiteSpace())
            {
                _logs.Add(log);
            }
            return this;
        }

        public LogContentBuilder Append(string fieldName, decimal oldValue, decimal newValue)
        {
            var log = fieldName.ToLog(oldValue, newValue);
            if (log.IsNotNullAndWhiteSpace())
            {
                _logs.Add(log);
            }
            return this;
        }

        public LogContentBuilder Append(string fieldName, decimal? oldValue, decimal? newValue)
        {
            var log = fieldName.ToLog(oldValue, newValue);
            if (log.IsNotNullAndWhiteSpace())
            {
                _logs.Add(log);
            }
            return this;
        }

        public LogContentBuilder Append(string fieldName, long oldValue, long newValue)
        {
            var log = fieldName.ToLog(oldValue, newValue);
            if (log.IsNotNullAndWhiteSpace())
            {
                _logs.Add(log);
            }
            return this;
        }

        public LogContentBuilder Append(string fieldName, DateTime oldValue, DateTime newValue)
        {
            var log = fieldName.ToLog(oldValue, newValue);
            if (log.IsNotNullAndWhiteSpace())
            {
                _logs.Add(log);
            }
            return this;
        }

        public LogContentBuilder Append(string fieldName, long? oldValue, long? newValue)
        {
            var log = fieldName.ToLog(oldValue, newValue);
            if (log.IsNotNullAndWhiteSpace())
            {
                _logs.Add(log);
            }
            return this;
        }

        public LogContentBuilder Append(string fieldName, int? oldValue, int? newValue)
        {
            var log = fieldName.ToLog(oldValue, newValue);
            if (log.IsNotNullAndWhiteSpace())
            {
                _logs.Add(log);
            }
            return this;
        }

        public LogContentBuilder Append(string fieldName, int oldValue, int newValue)
        {
            var log = fieldName.ToLog(oldValue, newValue);
            if (log.IsNotNullAndWhiteSpace())
            {
                _logs.Add(log);
            }
            return this;
        }

        public LogContentBuilder Append(string fieldName, DateTime? oldValue, DateTime? newValue)
        {
            var log = fieldName.ToLog(oldValue, newValue);
            if (log.IsNotNullAndWhiteSpace())
            {
                _logs.Add(log);
            }
            return this;
        }

        public LogContentBuilder Append(string content)
        {
            if (content.IsNotNullAndWhiteSpace())
            {
                _logs.Add(content);
            }
            return this;
        }

        public override string ToString() => ToString(",");

        public string ToString(string separator)
        {
            if (_logs.Any())
            {
                return string.Join(separator, _logs);
            }
            else
            {
                return string.Empty;
            }
        }
    }
}
